# 实现自动存款机

# 系统账户
atm_system = [{
    "user_name": "zhang",
    "password": "111111",
    "money": 1000.0
}, {
    "user_name": "li",
    "password": "222222",
    "money": 2000.0
}, {
    "user_name": "wang",
    "password": "333333",
    "money": 3000.0
}]


def login(user_name, password):
    """
    登录函数，验证是否存在账户以及对应密码是否正确
    """
    exist_user = False  # 账户是否存在
    for user in atm_system:
        if user["user_name"] == user_name:
            exist_user = True
            if user["password"] == password:
                print("登录成功！")
                return user
            else:
                print("密码错误！")
                return False
    if not exist_user:
        print("用户不存在！")
        return False


while True:
    #输出初始界面
    print("=" * 12 + "ATM系统" + "=" * 12)
    print(" " * 4 + "1：登录" + " " * 2 + "2：退出")
    print("=" * 31)
    print()
    operate_type = input("请选择操作...")
    if operate_type == "1":
        # 登录
        user_name = input("请输入用户名：")
        password = input("请输入密码：")
        print()
        user = login(user_name, password)
        print()
        if user:
            while True:
                # 用户内操作
                print("=" * 10 + "用户：" + user["user_name"] + "=" * 10)
                print(" " * 2 + "A：查询余额")
                print(" " * 2 + "B：存款")
                print(" " * 2 + "C：取款")
                print(" " * 2 + "D：登出")
                print("=" * 31)
                print()
                user_operate = input("请选择操作...")
                if user_operate == "A":
                    print()
                    print(" " * 2 + "当前账户余额：" + str(user["money"]))
                    print()
                    continue
                elif user_operate == "B":
                    print()
                    add_money = input("请输入存款数额：")
                    print()
                    try:
                        user["money"] += float(add_money)
                    except:
                        print("非法输入，请输入正确金额！")
                        continue
                    print("用户：{0}，存入：{1}元，余额：{2}元".format(
                        user["user_name"], add_money, user["money"]))
                    print()
                    continue
                elif user_operate == "C":
                    print()
                    remove_money = input("请输入存款数额：")
                    print()
                    try:
                        user["money"] -= float(remove_money)
                    except:
                        print("非法输入，请输入正确金额！")
                        continue
                    print("用户：{0}，取出：{1}元，余额：{2}元".format(
                        user["user_name"], remove_money, user["money"]))
                    print()
                    continue
                elif user_operate == "D":
                    print()
                    print("=" * 10 + "用户登出！" + "=" * 11)
                    print()
                    break
                else:
                    print()
                    print("无效的选择！")
                    print()
        else:
            continue
        pass
    elif operate_type == "2":
        # 退出
        print()
        print("=" * 10 + "退出系统！" + "=" * 11)
        print()
        break
    else:
        print()
        print("无效的选择！")
        print()
